<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/displaytag.tld" prefix="display"%>

<script type="text/javascript" src="<%= request.getContextPath() + "/js/popUP.js" %>"></script>
<link href="<%= request.getContextPath() + "/css/bloques.css" %>" rel="stylesheet" type="text/css">
<script type="text/javascript">
function hacerBusqueda() {
	var formBenSede = document.forms['formBeneficiosSede'];
	var prog = formBenSede.codePrograma.value;
	var sede = formBenSede.code.value;

	if (sede == 'nulo' && prog == 'nulo') {
		alert('Por favor seleccione un programa o una sede');
		return;
	}

	if (formBenSede.dia1.selectedIndex <= 0 || formBenSede.mes1.selectedInex <= 0 || formBenSede.ano1.selectedIndex <= 0) {
		alert('Debe indicar una fecha inicial');
		return;
	}

	if (formBenSede.dia2.selectedIndex <= 0 || formBenSede.mes2.selectedIndex <= 0 || formBenSede.ano2.selectedIndex <= 0) {
		alert('Debe indicar una fecha final');
		return;
	}

	fechainicial = formBenSede.ano1.value + formBenSede.mes1.value + formBenSede.dia1.value;
	fechafinal = formBenSede.ano2.value + formBenSede.mes2.value + formBenSede.dia2.value;

	if (fechainicial > fechafinal) {
		alert('La fecha inicial debe ser menor a la fecha final');
		return;
	}

	formBenSede.fechaInicial.value = formBenSede.dia1.value + '/' + formBenSede.mes1.value + '/'
			+ formBenSede.ano1.value;

	formBenSede.fechaFinal.value = formBenSede.dia2.value + '/'+formBenSede.mes2.value + '/'
			+ formBenSede.ano2.value;

	enviarForma('BuscarTodo');
}

function enviarForma(accion){
	var formBenSede = document.forms['formBeneficiosSede'];
	formBenSede.accion.value = accion;
	formBenSede.submit();
}

function habilita(nombre){
	document.forms['formBeneficiosSede'].elements[nombre].disabled = false;
}

function anyoBisiesto(anyo) {
	var agno = anyo;
	if (agno < 100) {
		agno = agno + 1900;
	}

	if (agno % 4 != 0) {
		return false;
	} else {
		if (agno % 100 == 0) {
			if (agno % 400 == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}
}

function validarFecha(vdia, vmes, vano) {
	var formBenSede = document.forms['formBeneficiosSede'];

	var dia = formBenSede.elements[vdia];
	var mes = formBenSede.elements[vmes];
	var anyo = formBenSede.elements[vano];
	var diasFeb;

	if (dia.selectedIndex > 0 && mes.selectedIndex > 0 && anyo.selectedIndex > 0) {
		if ((isNaN(dia.value) == true) || (isNaN(mes.value) == true) || (isNaN(anyo.value) == true) ) {
			alert("LA fecha introducida debe estar formada s?lo por n?meros");
			return;
		}

		if (anyoBisiesto(anyo.value)) {
			diasFeb = 29;
		} else {
			diasFeb = 28;
		}

		if ((mes.value < 1) || (mes.value > 12)) {
			alert("El mes introducido no es valido. Por favor, introduzca un mes correcto");
			mes.selectedIndex = 0;
			mes.focus();
			return;
		}

		if ((mes.value == 2) && ((dia.value < 1 ) || (dia.value > diasFeb))) {
			alert("El dia introducido no es valido. Por favor, introduzca un dia correcto");
			dia.selectedIndex = 0;
			dia.focus();
			return;
		}

		if (((mes.value == 1) || (mes.value == 3) || (mes.value == 5) || (mes.value == 7) || (mes.value == 8) || (mes.value == 10) || (mes.value == 12))
				&& ((dia.value < 1) || (dia.value > 31))) {
			alert("El dia introducido no es valido. Por favor, introduzca un dia correcto");

			dia.focus();

			return;
		}

		if (((mes.value == 4) || (mes.value == 6) || (mes.value == 9) || (mes.value == 11)) && ((dia.value < 1) || (dia.value > 30))) {
			alert("El dia introducido no es valido. Por favor, introduzca un dia correcto");
			dia.selectedIndex = 0;
			dia.focus();
			return;
		}

		if ((anyo.value < 1990) || (anyo.value > 2040)) {
		   alert("El a?o introducido no es valido. Por favor, introduzca un a?o entre 1900 y 2040");
		   anyo.selectedIndex = 0;
		   anyo.focus();
		   return;
		}
	}
 }

function generarReporte(){ 
	var formBenSede = document.forms['formBeneficiosSede'];
	var marcaReporte = document.getElementsByName('generar');
	var isCheck = false;

	for(var i = marcaReporte.length - 1; i >= 0 ; i--) {
		if (marcaReporte[i].checked == true) {
			formBenSede.posicion.value = i;
			isCheck = true;
			break;
		}
	}

	if (isCheck) {
		formBenSede.action = '<%=request.getContextPath()+"/Reportes/Detallados/SeleccionEG.do"%>';
		formBenSede.submit();
	} else {
		alert("Por favor seleccione una opción para generar el reporte");
	}
}
</script>
<script src="<%=request.getContextPath()%>/js/displaytag.js" type="text/javascript"></script>
<% 
	String codeSede = request.getParameter("code") == null ? "nullo" : request.getParameter("code"); 
	String codeArea = request.getParameter("codearea") == null ? "nulo" : request.getParameter("codearea");
	String codePrograma = request.getParameter("codePrograma") == null ? "nulo" : request.getParameter("codePrograma");
	String codeSubPrograma = request.getParameter("codeSubPrograma") == null ? "nulo" : request.getParameter("codeSubPrograma"); 
	String codigovfl = request.getParameter("codigovfl") == null ? "nulo" : request.getParameter("codigovfl");
%>
<form name="formBeneficiosSede" action="<%=request.getContextPath()+"/Reportes/Detallados/ReportDetEnteGestor.do"%>" method="post">
  <input type="hidden" name="fechaInicial" value=""/>
  <input type="hidden" name="fechaFinal" value=""/>
<table cellspacing="3" cellpadding="2" border="0" width="70%" align="center">
  <tr>
	<td class="titulosenblanco" colspan="3">
	  <div align="center">
		<strong>Reporte Detallado Por Ente Gestor</strong>
	  </div>
	</td>
  </tr>
  <tr>
	<td class="titulosenblanco" colspan="3">&nbsp;</td>
  </tr>
  <tr>
	<td align="center" class="celdatabla" colspan="3">Seleccione los criterios de busqueda</td>
  </tr>
  <tr>
	<td class="titulosenblanco" colspan="3">&nbsp;</td>
  </tr>
  <tr>
	<td class="celdatabla" width="31%">Sede:</td>
	<td width="44%">
	  <logic:present scope="request" name="sedes">
		<div align="left">
		  <logic:iterate id="sede" name="sedes" length="1" type="com.cs.beneficios.business.model.SedeVO">
			<html:select name="sede" property="code" title="Sede" styleClass="form" value="<%=codeSede%>">
			  <option value="nulo" selected="selected">Seleccione una sede</option>
			  <html:options name="sede" labelName="codeSede" labelProperty="nombre" property="code" collection="sedes"/>
			</html:select>
		  </logic:iterate>
		</div>
	  </logic:present>
	  <logic:notPresent scope="request" name="sedes">
		<div align="center"><%=request.getAttribute("msgSedes")%></div>
	  </logic:notPresent>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
  <tr>
	<td class="celdatabla" width="31%">Secretarias:</td>
	<td width="44%">
	  <logic:present scope="request" name="areas">
		<div align="left">
		  <logic:iterate id="area" name="areas" length="1" type="com.cs.beneficios.business.model.AreaVO">
			<html:select name="area" property="codearea" title="Area" styleClass="form" value="<%=codeArea%>" onchange="enviarForma('areas');">
			  <option value="nulo" selected="selected">Seleccione una secretaria</option>
			  <html:options name="area" labelName="codeArea" labelProperty="nombre" property="codearea" collection="areas"/>
			</html:select>
		  </logic:iterate>
		</div>
	  </logic:present>
	  <logic:notPresent scope="request" name="areas">
		<div align="center"><strong><%=request.getAttribute("msgAreas")%></strong></div>
	  </logic:notPresent>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
  <tr>
	<td class="celdatabla" width="31%">Programa:</td>
	<td width="44%">
	  <logic:present scope="request" name="programas">
		<div align="left">
		  <logic:iterate id="programa" name="programas" length="1" type="com.cs.beneficios.business.model.ProgramaVO">
			<html:select name="programa" property="codePrograma" onchange="enviarForma('BuscarSubprogrmas');" title="Programa" styleClass="form" value="<%=codePrograma%>">
			  <option value="nulo">Seleccione un programa</option>
			  <html:options name="programas" labelName="codigo" labelProperty="nombre" property="code" collection="programas"/>
			</html:select>
		  </logic:iterate>
		</div>
	  </logic:present>
	  <logic:notPresent scope="request" name="programas">
		<div align="center"><strong><%=request.getAttribute("msgProgramas")%></strong></div>
	  </logic:notPresent>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
<%  if (codePrograma != null && !codePrograma.equals("nulo")) { %>
  <tr>
	<td class="celdatabla" width="31%">Beneficio:</td>
	<td width="44%">
	  <logic:present scope="request" name="subprogramas">
		<div align="left">
		  <logic:iterate id="subprograma" name="subprogramas" length="1" type="com.cs.beneficios.business.model.SubProgramaVO">
			<html:select name="subprograma" property="codeSubPrograma" onchange="enviarForma('BuscarSiTieneAtributo');" title="Subprograma" styleClass="form" value="<%=(String)codeSubPrograma%>">
			  <option value="nulo">Seleccione un subprograma</option>
			  <html:options name="subprogramas" labelName="codigo" labelProperty="nombre" property="code" collection="subprogramas"/>
			</html:select>
		  </logic:iterate>
		</div>
	  </logic:present>
	  <logic:notPresent scope="request" name="subprogramas">
		<div align="center"><%=request.getAttribute("msgSubprogramas")%></div>
	  </logic:notPresent>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
	<%  if (codeSubPrograma != null && !codeSubPrograma.equals("nulo")) { %>
  <logic:present name="Gestor">
  <tr>
	<td class="celdatabla" width="31%">Gestor:</td>
	<td>
	  <logic:iterate id="gestor" name="Gestor" length="1" type="com.cs.beneficios.business.model.ValorFlexibleVO">
		<html:select name="Gestor" property="codigovfl" title="Gestor" styleClass="form" value="<%=codigovfl%>">
		  <option value="nulo">Seleccione un gestor</option>
		  <html:options name="Gestor" labelName="codigovfl" labelProperty="codigovfl" property="codigovfl" collection="Gestor"/>
		</html:select>
	  </logic:iterate>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
  </logic:present>
	<% 
		} else {
			session.removeAttribute("Gestor");
			session.removeAttribute("codigovfl");
			session.removeAttribute("codigoAtrGestor");
		}
	%>
<%  } %>
  <tr>
	<td class="celdatabla" width="31%">Fecha inicial:</td>
	<td align="left">
	  <span class="celdatabla">dia</span> <select name="dia1" onchange="validarFecha('dia1', 'mes1', 'ano1')" class="form">
		<option value="nulo">---</option>
<%
	String diaSel1 = request.getParameter("dia1") == null ? "" : request.getParameter("dia1");
	for(int d = 1; d <= 31; d++) {
		String dia = String.valueOf(d);
		if (d < 10) dia = "0" + d;
		if (dia.equals(diaSel1)) {
%>
		<option value="<%=dia%>" selected><%=dia%></option>
<%      } else { %>
		<option value="<%=dia%>"><%=dia%></option>
<%
		}
	}
%>
	  </select>
	  <span class="celdatabla">mes</span> <select name="mes1" onchange="validarFecha('dia1', 'mes1', 'ano1')" class="form">
		<option value="nulo">---</option>
<% 
	String mesSel1 = request.getParameter("mes1") == null ? "" : request.getParameter("mes1");
	for(int m = 1; m <= 12; m++) {
		String mes = String.valueOf(m);
		if (m < 10) mes = "0" + m;
		if (mes.equals(mesSel1)) {
%>
		<option value="<%=mes%>" selected><%=mes%></option>
<%      } else { %>
		<option value="<%=mes%>"><%=mes%></option>
<%
		}
	}
%>
	  </select>
	  <span class="celdatabla">a&ntilde;o</span> <select name="ano1" onchange="validarFecha('dia1', 'mes1', 'ano1')" class="form">
		<option value="nulo">-----</option>
<%
	String ano1 = request.getParameter("ano1") == null ? "" : request.getParameter("ano1");
	for (int a = 1990; a <= 2040; a++) {
		String ano = String.valueOf(a);
		if (ano.equals(ano1)) {
%>
		<option value="<%=ano%>" selected><%=ano%></option>
<%      } else { %>
		<option value="<%=ano%>"><%=ano%></option>
<%
		}
	}
%>
	  </select>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
  <tr>
	<td class="celdatabla" width="31%">Fecha final:</td>
	<td align="left">
	  <span class="celdatabla">dia</span> <select name="dia2" onchange="validarFecha('dia2', 'mes2', 'ano2')" class="form">
		<option value="nulo">---</option>
<%
	String diaSel2 = request.getParameter("dia2") == null ? "" : request.getParameter("dia2");
	for (int d = 1; d <= 31; d++) {
		String dia = String.valueOf(d);
		if (d < 10) dia = "0" + d;
		if (dia.equals(diaSel2)) {
%>
		<option value="<%=dia%>" selected><%=dia%></option>
<%      } else { %>
		<option value="<%=dia%>"><%=dia%></option>
<%
		}
	}
%>
	  </select>
	  <span class="celdatabla">mes</span> <select name="mes2" onchange="validarFecha('dia2', 'mes2', 'ano2')" class="form">
		<option value="nulo">---</option>
<% 
	String mesSel2 = request.getParameter("mes2") == null ? "" : request.getParameter("mes2");
	for (int m = 1; m <= 12; m++) {
		String mes = String.valueOf(m);
		if (m < 10) mes = "0" + m;
		if (mes.equals(mesSel2)) {
%>
		<option value="<%=mes%>" selected><%=mes%></option>
<%      } else { %>
		<option value="<%=mes%>"><%=mes%></option>
<%
		}
	} 
%>
	  </select>
	  <span class="celdatabla">a&ntilde;o</span> <select name="ano2" onchange="validarFecha('dia2', 'mes2', 'ano2')" class="form">
	  <option value="nulo">-----</option>
<% 
	String ano2 = request.getParameter("ano2") == null ? "" : request.getParameter("ano2");
	for (int a = 1990; a <= 2040; a++) {
		String ano = String.valueOf(a);
		if (ano.equals(ano2)) {
%>
	  <option value="<%=ano%>" selected><%=ano%></option>
<%      } else {%>
	  <option value="<%=ano%>"><%=ano%></option>
<%
		}
	}
%>
	  </select>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
  <tr>
	<td class="celdatabla" width="31%">&nbsp;</td>
	<td width="44%">
	  <div align="center">
		  <input type="hidden" name="accion" value=""/>
		  <input type="button" value="buscar" name="buscar" onclick="hacerBusqueda();"/>
		<p>&nbsp;</p>
	  </div>
	</td>
	<td class="celdatabla" width="25%">&nbsp;</td>
  </tr>
  <logic:present scope="request" name="msg">
  <tr>
	<td colspan="3" align="center">
	  <div align="center"><%=request.getAttribute("msg")%></div>
	</td>
  </tr>
  </logic:present>
</table> 

<logic:present scope="request" name="errorfecha">
  <br>
  <div align="center" class="titulosenblanco"><strong><%=(String)request.getAttribute("errorfecha")%></strong></div>
  <br>
</logic:present> 

<logic:present scope="session" name="BeneficiosOtorgado">
  <table cellspacing="3" cellpadding="2" border="0" width="90%" align="center">
	<tr>
	  <td class="titulosenblanco"><div align="center">Beneficios Otorgado</div></td>
	</tr>
	<tr>
	<td>
	<display:table name="sessionScope.BeneficiosOtorgado" pagesize="20" id="beneotot" cellpadding="3" cellspacing="1" style="width:100%" class="bordestable">
	  <display:column headerClass="headerTable" title="Programa" class="Numero" sortable="true" >
		<div align="center">
		  <bean:write name="beneotot" property="nombrePrograma" /> 
		</div>
	  </display:column>
	  <display:column headerClass="headerTable" title="Beneficio" class="Numero" sortable="true" >
		<div align="center">
		  <bean:write name="beneotot" property="nombreSubprograma"/> 
		</div>
	  </display:column>
	  <display:column headerClass="headerTable" title="Sede" class="Numero" sortable="true">
		<div align="center">
		  <bean:write name="beneotot" property="nombreSede"/>
		</div>
	  </display:column>
	  <display:column headerClass="headerTable" title="Cantidad" class="Numero" sortable="true" >
		<div align="center">
		  <bean:write name="beneotot" property="cantidad"/>
		</div>
	  </display:column>
	  <display:column headerClass="headerTable" title="Generar Reporte" class="Numero">
		<div align="center">
		  <input type="radio" name="generar" value="<bean:write name="beneotot" property="codeSubprograma"/>"/>
		  <input type="hidden" name="prueba" value="<bean:write name="beneotot" property="codeSede"/>"/>
		</div>
	  </display:column>	
	</display:table>
	<p align="center">
	  <input type="hidden" name="posicion" value="0"/> 
	  <input type="button" name="" value="Generar" onclick="generarReporte()">
	</p>
	</td>
  </tr>
</table>
</logic:present>
</form>
